.\"
.\" pl0c - PL/0 compiler
.\"
.\" Copyright (c) 2021 Brian Callahan <bcallah@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd August 26, 2021
.Dt PL0C 1
.Os
.Sh NAME
.Nm pl0c
.Nd PL/0 compiler
.Sh SYNOPSIS
.Nm
.Sh DESCRIPTION
.Nm
is a PL/0 compiler.
.Nm
reads input from
.Ar stdin
and outputs to
.Ar stdout .
.Sh GRAMMAR
The PL/0 grammar is as follows:
.Bd -literal
program		= block "." .
block		= [ "const" id "=" number { "," id "=" number } ";" ]
		  [ "var" id [ array ] { "," id  [ array ] } ";" ]
		  { "forward" id ";" }
		  { "procedure" id ";" block ";" } statement .
statement	= [ id ":=" expression
		  | "call" id
		  | "begin" statement { ";" statement } "end"
		  | "if" condition "then" statement
		  | "while" condition "do" statement
		  | "readInt" [ "into" ] id
		  | "readChar" [ "into" ] id
		  | "writeInt" expression
		  | "writeChar" expression
		  | "writeStr" ( id | string )
		  | "exit" expression ] .
condition	= "odd" expression
		| expression ( comparator ) expression .
expression	= [ "+" | "-" | "not" ] term { ( "+" | "-" | "or" ) term } .
term		= factor { ( "*" | "/" | "mod" | "and" ) factor } .
factor		= id
		| number
		| "(" expression ")" .
comparator	= "=" | "#" | "<" | ">" | "<=" | ">=" | "<>" .
array		= "size" number .
.Ed
.Sh EXIT STATUS
The
.Nm
utility exits 0 on success, and >0 if an error occurs.
.Sh EXAMPLES
The following program prints the first 16 factorials:
.Bd -literal -offset indent
var n, f;
begin
  n := 0;
  f := 1;
  while n # 16 do
  begin
    n := n + 1;
    f := f * n;
    writeInt f;
    writeChar '\\n'
  end;
end.
.Ed
.Pp
The output of
.Nm
can be piped into a C compiler to produce an executable, like so:
.Pp
.Dl pl0c < file.pl0 | cc -O -o file -x c -
.Sh AUTHORS
.Nm
was written by
.An Brian Callahan Aq Mt bcallah@openbsd.org .
